package igor.mass;

import java.util.Vector;

import igor.mass.model.AndroidCommand;
import igor.mass.model.AndroidDevice;
import igor.mass.model.AndroidRoom;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpadableAdapter extends BaseExpandableListAdapter {
	private AndroidRoom room;
	private Vector<AndroidDevice> devices;
    private Activity parentActivity;
    private CustomTouchListener touchListener;
    private OnClickListener clickListener;
	
	public ExpadableAdapter(Activity parentActivity, AndroidRoom room){
		this.parentActivity=parentActivity;
		this.room = room;
		this.devices = this.room.devices;
		touchListener = new CustomTouchListener();
		clickListener = (OnClickListener) parentActivity;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		Vector<AndroidCommand> commands = devices.get(groupPosition).commands;
		return commands.get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    public int getChildrenCount(int groupPosition) {
    	Vector<AndroidCommand> commands = devices.get(groupPosition).commands;
    	return commands.size();
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
    	
    	TextView textView;
    	if (convertView == null){
    		textView = getGenericView();
    	} else {
    		textView = (TextView) convertView;
    	}
        textView.setText(getChild(groupPosition, childPosition).toString());
        
        //textView.setHeight(100);
        //textView.setBackgroundColor(0xFF323232);
        textView.setTextSize(16);
        textView.setMaxHeight(10);
        textView.setBackgroundResource(R.drawable.list_background_child);
        textView.setPadding(36, 0, 0, 0);
        textView.setOnClickListener(clickListener);
        textView.setTag(R.id.device,groupPosition);
        textView.setTag(R.id.command,childPosition);
        return textView;
    }
    
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
    	TextView textView;
    	if (convertView == null){
    		textView = getGenericView();
    	} else {
    		textView = (TextView) convertView;
    	}
        
        textView.setText(getGroup(groupPosition).toString());
        //textView.setTypeface(null, Typeface.BOLD);
        textView.setTextSize(20);
        //textView.setHeight(150);
        textView.setMaxHeight(64);
        textView.setBackgroundResource(R.drawable.list_background_parent);
        textView.setPadding(6, 0, 0, 0);
        return textView;
    }
    
    public TextView getGenericView() {
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 64);

        TextView textView = new TextView(parentActivity);
        textView.setLayoutParams(lp);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setSingleLine();
        //textView.setBackgroundColor(0xFF252525);
        
        return textView;
    }

    public Object getGroup(int groupPosition) {
    	AndroidDevice device= devices.get(groupPosition);
        return device;
    }

    public int getGroupCount() {
    	return devices.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }



    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public boolean hasStableIds() {
        return true;
    }


}
